import random
import sys
import numpy as np

zeroP = 0.1

def generate_sparse_matrix(M, N, Max):
    matrix = []
    for i in range(0, M):
        matrix.append([])
        for j in range(0, N):
            if random.random() > zeroP:
                matrix[i].append(random.randint(1, Max))
            else:
                matrix[i].append(0)
    return matrix


def generate_matrix(M, N, Max):
    matrix = []
    for i in range(0, M):
        matrix.append([])
        for j in range(0, N):
            matrix[i].append(random.randint(0, Max))
    return matrix


def write_matrix(f, matrix, M, N):
    f.write("%d" % M)
    f.write(' ')
    f.write("%d" % N)
    f.write('\n')
    for i in range(0, M):
        for j in range(0, N):
            f.write("%.8f" % matrix[i][j])
            if j != N - 1:
                f.write(' ')
        f.write('\n')
    f.flush()


def write_matrix_numpy(f, matrix, M, N):
    f.write("%d" % M)
    f.write(' ')
    f.write("%d" % N)
    f.write('\n')
    for i in range(0, M):
        for j in range(0, N):
            f.write("%.8f" % matrix[i,j])
            if j != N - 1:
                f.write(' ')
        f.write('\n')
    f.flush()



def generate_with_specific_name(M1, N1, Max, sparse, name):
    global matrix_1, matrix_2, filename
    if sparse:
        matrix_1 = generate_sparse_matrix(M1, N1, Max)
    else:
        matrix_1 = generate_matrix(M1, N1, Max)

    with open(name+".txt", 'w+') as f:
        write_matrix(f, matrix_1, M1, N1)


def generate(M1, N1, Max, sparse):
    global matrix_1, matrix_2, filename
    if sparse:
        matrix_1 = generate_sparse_matrix(M1, N1, Max)
    else:
        matrix_1 = generate_matrix(M1, N1, Max)

    with open(str(M1) + "_" + str(N1) +".txt", 'w+') as f:
        write_matrix(f, matrix_1, M1, N1)


def process_data(fname, sheet):
    import pandas as pd
    import re
    df = pd.read_excel(fname, sheet_name=sheet)
    df = np.array(df.iloc[:,1:])
    # with open(re.sub("xlsx","mtx", fname), "w+") as f:
    #     f.write("{} {}\n".format(df.shape[0], df.shape[1]))
    #     s = ""
    #     for i in range(df.shape[0]):
    #         for j in range(1,df.shape[1]):
    #             s += str(df[i,j])+ " "
    #         s += "\n"
    #     f.write(s)
    #     f.close()
    return df


def kronecker_product(mat, prod):
    rows = []
    for i in range(prod.shape[0]):
        for row in mat[:]:
            new_row = []
            # print(row.shape)
            for j in range(prod.shape[1]):
                new_row += [prod[i,j]*x for x in row]
            rows.append(new_row)
    return np.array(rows)

if __name__ == "__main__":
    import sys
    global isSparse
    isSparse = False
    scale = 3
    i = int(sys.argv[1])
    generate_with_specific_name(i, i, 100, False, "tmp")
    # mat = process_data("USEEIO.xlsx", "A")
    # prod = np.random.rand(scale*scale).reshape((scale,scale)) * 1e3
    # emat = kronecker_product(mat,prod)
    # with open("USEEIO.mtx", "w+") as f:
    #     write_matrix_numpy(f, emat, emat.shape[0], emat.shape[1])
    #     f.close()